package com.baseweb.webadmin.service.impl;


import com.baseweb.webadmin.core.model.dao.RoleRescDao;
import com.baseweb.webadmin.core.model.mapper.RescMapper;
import com.baseweb.webadmin.core.model.mapper.RescRoleMapper;
import com.baseweb.webadmin.core.model.pojo.Resc;
import com.baseweb.webadmin.core.model.pojo.RescExample;
import com.baseweb.webadmin.core.model.pojo.RescRoleExample;
import com.baseweb.webadmin.core.model.pojo.RescRoleKey;
import com.baseweb.webadmin.service.RoleRescService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleRescServiceImpl implements RoleRescService {
    @Autowired
    private RoleRescDao roleRescDao;
    @Autowired
    private RescMapper rescMapper;
    @Autowired
    private RescRoleMapper rescRoleMapper;

    @Override
    public Resc selectRescById(Integer rescId) {
        return rescMapper.selectByPrimaryKey(rescId);
    }

    @Override
    public List<RescRoleKey> selectRescRoleKeyByRoleId(Integer roleId) {
        RescRoleExample rescRoleExample = new RescRoleExample();
        rescRoleExample.createCriteria().andRoleIdEqualTo(roleId);

        return rescRoleMapper.selectByExample(rescRoleExample);
    }

    @Override
    public Resc selectRescByNameAndResString(String name, String resString) {
        Resc resc = new Resc();
        resc.setName(name);
        resc.setResString(resString);
        return roleRescDao.selectRescByNameAndResString(resc);
    }

    @Override
    public Boolean selectRescByName(String name) {
        RescExample rescExample = new RescExample();
        rescExample.createCriteria().andNameEqualTo(name);
        List<Resc> rescList = rescMapper.selectByExample(rescExample);
        if (rescList.size() > 0) {
            return true;
        } else {
            return false;
        }

    }

    @Override
    public int updateResc(Resc resc) {
        return rescMapper.updateByPrimaryKeySelective(resc);
    }

    @Override
    public List<Resc> selectRescs() {
        return roleRescDao.selectRescs();
    }

    @Override
    public int insertResc(Resc resc) {
        return rescMapper.insertSelective(resc);
    }

    @Override
    public int insertRescRole(RescRoleKey rescRole) {
        return rescRoleMapper.insert(rescRole);
    }

    @Override
    public int deleteRescById(Integer rescId) {
        return rescMapper.deleteByPrimaryKey(rescId);
    }

    @Override
    public int deleteRescRoleByRescId(Integer rescId) {
        RescRoleExample rescRoleExample = new RescRoleExample();
        rescRoleExample.createCriteria().andRescIdEqualTo(rescId);
        return rescRoleMapper.deleteByExample(rescRoleExample);
    }

    @Override
    public int deleteRescRoleByRoleId(Integer roleId) {
        RescRoleExample rescRoleExample = new RescRoleExample();
        rescRoleExample.createCriteria().andRoleIdEqualTo(roleId);
        return rescRoleMapper.deleteByExample(rescRoleExample);
    }

    @Override
    public List<Resc> getRescByRoleId(Integer roleId, Integer fatherId) {
        List<RescRoleKey> rescRoleKey = selectRescRoleKeyByRoleId(roleId);
        List<Integer> rescIds = new ArrayList<Integer>();
        for (RescRoleKey roleKey : rescRoleKey) {
            rescIds.add(roleKey.getRescId());
        }

        if(rescIds.size() > 0) {
            RescExample rescExample = new RescExample();
            rescExample.createCriteria().andIdIn(rescIds).andFatherIdEqualTo(fatherId).andShowMenuEqualTo(1);
            return rescMapper.selectByExample(rescExample);
        } else {
          return new ArrayList<Resc>();
        }
    }

    @Override
    public List<Resc> getRescByFatherId(Integer fatherId) {
        RescExample rescExample = new RescExample();
        rescExample.createCriteria().andFatherIdEqualTo(fatherId);
        return rescMapper.selectByExample(rescExample);
    }

    @Override
    public List<Resc> getRescByRole(Integer id) {
        List<Resc> rescs = roleRescDao.getRescByRole(id);       //通过角色获得资源

        //获取子节点
//        Map map = new HashMap();
//        List<Resc> childRescs = new ArrayList<Resc>();
//        for (Resc resc : rescs) {
//            if (map.get(resc.getId()) == null) {
//                map.put(resc.getId(), true);
//                childRescs.addAll(getChildRescs(resc.getId(), new HashMap()));
//            }
//        }
//
//        //加入子节点
//        for (Resc r : childRescs) {
//            if (map.get(r.getId()) == null) {
//                map.put(r.getId(), true);
//                rescs.add(r);
//            }
//        }

        return rescs;
    }

    @Override
    public int deleteByPrimaryKey(RescRoleKey key) {
        return rescRoleMapper.deleteByPrimaryKey(key);
    }

    /**
     * 根据资源id获取所以资源
     *
     * @param rescIds
     * @return
     */
    @Override
    public List<Resc> getRescByIds(List<Integer> rescIds) {
          RescExample rescExample = new RescExample();
          RescExample.Criteria criteria = rescExample.createCriteria();
          criteria.andIdIn(rescIds);
        return rescMapper.selectByExample(rescExample);
    }

//
//    /**
//     * 获取祖先节点
//     *
//     * @param id
//     * @return
//     */
//    private List<Resc> getParentRescs(Long id) {
//        if (id == 0) {
//            return new ArrayList<Resc>();
//        } else {
//            List<Resc> rescs = new ArrayList<Resc>();
//            while (true) {
//                Resc resc = rescMapper.selectByPrimaryKey(id);
//                if (resc.getFatherId() == 0) {
//                    rescs.add(resc);
//                    return rescs;
//                } else {
//                    if (rescs.contains(resc)) {//数据有误  存在死循环
//                        return rescs;
//                    } else {
//                        rescs.add(resc);
//                        id = resc.getFatherId();
//                    }
//                }
//            }
//        }
//    }
//
//    //获取指定的所有子孙节点
//    public List<Resc> getChildRescs(Long id, Map map) {
//        RescExample example = new RescExample();
//        RescExample.Criteria criteria = example.createCriteria();
//        criteria.andFatherIdEqualTo(id);
//
//        List<Resc> rescs = rescMapper.selectByExample(example);
//
//        List<Resc> list = new ArrayList<Resc>();
//        if (rescs != null && rescs.size() > 0) {
//            list.addAll(rescs);
//            for (Resc resc : rescs) {
//                if(map.get(resc.getId()) == null) {
//                    map.put(resc.getId(), true);
//                    list.addAll(getChildRescs(resc.getId(), map));
//                }
//            }
//        }
//
//        return list;
//    }
}
